package com.wjx.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjx.dtos.ResponseResult;
import com.wjx.dtos.UserDTO;
import com.wjx.euums.AppHttpCodeEnum;
import com.wjx.pojo.Accounts;
import com.wjx.pojo.Customers;
import com.wjx.pojo.Withdrawals;
import com.wjx.service.AccountsService;
import com.wjx.service.CustomersService;
import com.wjx.service.WithdrawalsService;
import com.wjx.mapper.WithdrawalsMapper;
import com.wjx.utils.ThreadLocalUtil;
import com.wjx.vos.SaveVO;
import com.wjx.vos.WithDrawalVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author WJX
* @description 针对表【withdrawals】的数据库操作Service实现
* @createDate 2024-06-25 17:08:40
*/
@Service
@Slf4j
public class WithdrawalsServiceImpl extends ServiceImpl<WithdrawalsMapper, Withdrawals>
    implements WithdrawalsService{
    @Autowired
    private CustomersService customersService;
    @Autowired
    private AccountsService accountsService;

    @Override
    public ResponseResult drawmoney(Withdrawals withdrawals) {
        // 先判断账户是否存在
        UserDTO user = ThreadLocalUtil.getUser();
        QueryWrapper<Customers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Name", user.getName());
        List<Customers> list = customersService.list(queryWrapper);
        if (list.isEmpty() || list.size() < 1){
            log.info("用户不存在");
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 判断余额是否足够
        Customers customers = list.get(0);
        Accounts accounts = new Accounts();
        Accounts accounts1 = accountsService.getOne(new QueryWrapper<Accounts>().eq("CustomerID", customers.getCustomerid()));
        Date openingdate = accounts1.getOpeningdate();
        long between = DateUtil.between(openingdate, DateUtil.date(), DateUnit.DAY);
        String accounttype = accounts1.getAccounttype();
        BigDecimal rate = new BigDecimal(0.00);

        rate=accounts1.getAccounttype().equals("1") ? new BigDecimal(0.01) : new BigDecimal(0.02);
        if (accounttype.equals("3")) {
            rate = new BigDecimal(0.00);
        }
        for (long i = 0; i < between; i++) {
            accounts1.setBalance(accounts1.getBalance().add(accounts1.getBalance().multiply(rate)));
        }
        if (accounts1.getBalance() .compareTo(withdrawals.getAmount())==-1){
            log.info("余额不足");
            return ResponseResult.errorResult(AppHttpCodeEnum.MONEY_NOT_ENOUGH);
        }
        // 余额足够，开始提现
        withdrawals.setAccountid(accounts1.getAccountid());
        withdrawals.setWithdrawaldate(DateUtil.date());
        boolean result = save(withdrawals);
        if (result){
            log.info("提现成功");
            accounts1.setBalance(accounts1.getBalance().subtract(withdrawals.getAmount()));
            accountsService.updateById(accounts1);
            WithDrawalVO drawalVO = new WithDrawalVO();
            drawalVO.setBalance(accounts1.getBalance());
            return ResponseResult.okResult(drawalVO);
        }else {
            log.info("提现失败");
            return ResponseResult.errorResult(AppHttpCodeEnum.FAILURE);
        }

    }

}




